package cn.iocoder.yudao.module.system.tcpserver.protocol1;

import cn.hutool.core.util.HexUtil;
import cn.iocoder.yudao.module.system.tcpserver.pojo.ZkPacketFrame;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class ZkProtocolEncoder extends MessageToByteEncoder<ZkPacketFrame> {


    /**
     *
     */
    public ZkProtocolEncoder() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param outboundMessageType
     */
    public ZkProtocolEncoder(Class<? extends ZkPacketFrame> outboundMessageType) {
        super(outboundMessageType);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param preferDirect
     */
    public ZkProtocolEncoder(boolean preferDirect) {
        super(preferDirect);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param outboundMessageType
     * @param preferDirect
     */
    public ZkProtocolEncoder(Class<? extends ZkPacketFrame> outboundMessageType,
                             boolean preferDirect) {
        super(outboundMessageType, preferDirect);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, ZkPacketFrame msg,
                          ByteBuf out) throws Exception {
        if (msg == null) {
            throw new Exception("The encode message is null");
        }

        out.writeBytes(msg.packet());

        //转换为16进制字符
        log.info("server to client data:{}", HexUtil.encodeHexStr(msg.packet()));
    }

}
